define(['jquery','jquery-weui','weui','common'], function ($,JqueryWuei,Weui) {
    var Controller = {
        index: function () {
            var storage_name = 'report_storage';
            console.log(window.localStorage);
            var tel_value = '';
            var nickname_value = '';
            if(window.localStorage['entry_tel'])
            {
                tel_value = window.localStorage['entry_tel'];
            }
            else
            {
                if(window.localStorage[storage_name] ){
                    var localMsg ;
                    localMsg=JSON.parse(window.localStorage[storage_name]);
                    // console.log(localMsg);
                    var item =  Controller.api.findElem(localMsg, "id", "tel");
                    if(item != -1)
                    {
                        tel_value = item.value;
                    }
                }
            }

            if(window.localStorage['entry_nickname'])
            {
                nickname_value = window.localStorage['entry_nickname'];
            }
            else
            {
                if(window.localStorage[storage_name] ){
                    var localMsg ;
                    localMsg=JSON.parse(window.localStorage[storage_name]);
                    // console.log(localMsg);
                    var item =  Controller.api.findElem(localMsg, "id", "nickname");
                    if(item != -1)
                    {
                        nickname_value = item.value;
                    }
                }
            }


            $("#tel").val(tel_value);
            $("#nickname").val(nickname_value);

            $('#formSubmitBtn').click(function () {
                // event.preventDefault();
                Weui.form.validate('#form1', function (error) {
                    if (!error) {


                        $.ajax({
                            //几个参数需要注意一下
                            type: "POST",//方法类型
                            dataType: "json",//预期服务器返回的数据类型
                            url: "/index.php/index/teacherentry" ,//url
                            data: $('#form1').serialize(),
                            success: function (ret) {
                                if(ret.code == 1)
                                {
                                    window.localStorage['entry_tel'] = $("#tel").val();
                                    window.localStorage['entry_nickname'] = $("#nickname").val();
                                    $.alert({
                                        title: ret.msg,
                                        text: ret.data.nickname+'您好:<br>您入校时间为：'+ret.data.create_time+"<br><div style='color:red;margin-top:5px;'>请将此页面提供给保卫处工作人员作为进校登记凭证</div>",
                                        onOK: function () {
                                              WeixinJSBridge.invoke('closeWindow', {}, function (res) { });

                                        }
                                    });
                                }
                                else if(ret.code == 0)
                                {
                                    $.alert({
                                        title: '提示',
                                        text:  ret.msg,
                                        onOK: function () {
                                            setbutton(1);
                                        }
                                    });
                                }

                            },
                            error : function(error) {
                                console.log(error);
                                alert(JSON.stringify(error))
                                $.alert({
                                    title: '提示',
                                    text: '网络异常,请再次提交！',
                                    onOK: function () {
                                        setbutton(1);
                                    }
                                });
                                // alert("异常！");
                            }
                        });
                    }

                });
                return false;
            });
        },
        api: {
            findElem: function(arrayToSearch, attr, val)
            {
                for (var i = 0; i < arrayToSearch.length; i++) {
                // alert(arrayToSearch[i][attr]);
                // alert(val);
                    if (arrayToSearch[i][attr] == val) {
                      return arrayToSearch[i];
                    }
                }
                return -1;
            }

        }
    }
    return Controller;
})
